package feedback_service

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"path"
	"peilian-api/app/global/common"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/errors"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
	"peilian-api/utils/function"
	"peilian-api/utils/sdk"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
)

type FeedbackService struct{}

func (fds *FeedbackService) FeedbackSubmit(ctx *gin.Context, fbs model.FeedBackSub) *errors.ErrRes {
	uid, exists := ctx.Get("uid")
	if !exists {
		return errors.NewErrInfo(errors.SessionUidErrorCode, fmt.Errorf("uid错误,请登录后尝试"))
	}
	images, _ := json.Marshal(fbs.Images)
	videos, _ := json.Marshal(fbs.Videos)
	if err := new(tables.FeedBack).InsertFeedBack(
		tables.FeedBack{
			UserId:  uid.(uint),
			Type:    fbs.Type,
			Content: fbs.Content,
			Images:  string(images),
			Videos:  string(videos),
		}); err != nil {
		return errors.NewErrInfo(errors.DatabaseMysqlErrorCode, fmt.Errorf("插入失败,请登录后尝试"))
	}
	return nil
}

func (fds *FeedbackService) FeedBackUpload(ctx *gin.Context, fbt model.FeedBackType) (int64, *errors.ErrRes) {
	uid, exists := ctx.Get("uid")
	if !exists {
		return 0, errors.NewErrInfo(errors.SessionUidErrorCode, fmt.Errorf("uid错误,请登录后尝试"))
	}
	_, f, err := ctx.Request.FormFile("filename")
	if err != nil {
		return 0, errors.NewErrInfo(20001, fmt.Errorf("文件数据获取失败, "+err.Error()))
	}

	fileExt := strings.ToLower(path.Ext(f.Filename))

	fileName := function.CreateMD5(fmt.Sprintf("%s%s", f.Filename, time.Now().String())) + fileExt

	filePath := common.VpPath + fileName

	savePath := variable.Config.UfsInfo.BucketPrefix + filePath
	f2, err := f.Open()
	if err != nil {
		return 0, errors.NewErrInfo(20003, fmt.Errorf("上传异常, "+err.Error()))
	}
	fileData, err := ioutil.ReadAll(f2)
	if err != nil {
		return 0, errors.NewErrInfo(20004, fmt.Errorf("上传IO异常, "+err.Error()))
	}

	err = sdk.IOput(fileData, savePath, "")
	if err != nil {
		return 0, errors.NewErrInfo(20005, fmt.Errorf("上传IO失败, "+err.Error()))
	}

	row, err := new(tables.Vpdata).InsertVp(tables.Vpdata{Info: filePath, Type: tables.VpDataTypeMap[fbt.Type], UserId: uid.(uint)})
	if err != nil {
		return 0, errors.NewErrInfo(errors.DatabaseMysqlErrorCode, err)
	}
	return row, nil
}

func (fds *FeedbackService) FeedBackEvaluate(ctx *gin.Context, fbi model.FeedBackInfo) (int64, *errors.ErrRes) {
	info := fbi.Info
	typ := fbi.Type
	userId := fbi.UserId
	sid := fbi.Sid
	expandId := fbi.ExpandId
	uid, exists := ctx.Get("uid")
	if !exists {
		return 20011, errors.NewErrInfo(errors.SessionUidErrorCode, fmt.Errorf("uid错误,请登录后尝试"))
	}
	if userId != int64(uid.(uint)) {
		return 20022, errors.NewErrInfo(20022, fmt.Errorf("uid is error"))
	}
	if _, ok := tables.VpDataTypeMap[typ]; ok {
		typ = tables.VpDataTypeMap[typ]
	}
	var vp = tables.Vpdata{Info: info, Type: typ, UserId: uint(userId), Sid: uint(sid), ExpandId: uint(expandId)}
	s, err := new(tables.Vpdata).UpsertVp(vp)
	if err != nil {
		return 20023, errors.NewErrInfo(20023, fmt.Errorf(s))
	}
	return 0, nil
}
